package com.dmall.manager.controller;

import com.dmall.pojo.TbItem;
import com.dmall.manager.service.ItemService;
import com.dmall.framework.result.PageResult;
import com.dmall.framework.result.DmallResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.jms.*;


/**
 * 商品管理Controller
 * @version 1.0
 */
@Controller
@RequestMapping("/item")
public class ItemController {

    @Autowired
    private ItemService itemService;
    @Autowired
    private JmsTemplate jmsTemplate;
    @Resource
    private Destination topicDestination;


//    @RequestMapping(value = "/{itemId}",method = RequestMethod.GET)
//    @ResponseBody
//    public TbItem getItemById(@PathVariable Long itemId) {
//        TbItem tbItem = itemService.getItemById(itemId);
//        return tbItem;
//    }

    /**
     * 查询商品列表
     * @return
     */
    @RequestMapping(method = RequestMethod.GET )
    @ResponseBody
    public PageResult getItemList(Integer page, Integer rows){
        PageResult itemList = itemService.getItemList(page, rows);
        return itemList;
    }

    /**
     * 新建商品
     */
    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    public DmallResult addItem(TbItem item,String desc){
        DmallResult dmallResult = itemService.addItem(item, desc);
        //发送商品添加消息
        jmsTemplate.send(topicDestination, new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                TextMessage textMessage = session.createTextMessage(dmallResult.getData() + "");
                return textMessage;
            }
        });
        return dmallResult;
    }



}
